class_name HoldGunGravity
extends HoldGunBase

@onready var fire_light: PointLight2D = $FireLight
@export var fire_light_energy: float = 3.0
@export var fire_light_duration: float = 1.0
@onready var fire_gun_body_lamp: Node2D = $FireGunBodyLamp

# hit
@onready var hit_area: Area2D = $HitArea
var hit_targets: Array = []


func _ready() -> void:
	set_show_ui_bullet_info()
	
	
func _physics_process(delta: float) -> void:
	pass


func shoot():
	var player: Player = holder as Player
	var gun_config = item_data.item_config as ItemGunBaseConfig
	#var remain_bullet_count = player.player_data.get_bullet_count(gun_config.bullet_type)
	#if remain_bullet_count <= 0:
		#return
		
	#player.player_data.reduce_bullet(gun_config.bullet_type)
	update_bullet_count()
	
	tween_fire_light()
	tween_fire_gun_body_lamp()
	hit()
	AudioManager.play_sound("sfx_gravity_shoot")
	

func hit():
	var config = item_data.item_config as ItemGunBaseConfig
	var player = holder as Player
	
	hit_targets = []
	
	for index in range(shoot_ray_parent.get_child_count()):
		var hit_ray = shoot_ray_parent.get_child(index) as RayCast2D
		
		if hit_ray.is_colliding():
			var hit_target = hit_ray.get_collider() as Node2D
			
			if hit_target is Area2D:
				hit_target = hit_target.source
				
			if hit_target.has_method("add_buff") and hit_target not in hit_targets:
				hit_targets.append(hit_target)
				var buff_inst = hit_target.add_buff("buff_gravity_attack", holder) as BuffGravityAttack
				buff_inst.attack = config.attack + player.player_data.get_attack()
				buff_inst.critical_rate = config.critical_rate
				buff_inst.critical_bonus = config.critical_bonus
				buff_inst.knockback = config.knockback
				buff_inst.affect_vertical_velocity = config.affect_vertical_velocity
				buff_inst.cam_shake_duration = config.attack_cam_shake_duration
				buff_inst.cam_shake_strength = config.attack_cam_shake_intensity
				buff_inst.is_enemy = false
				

func tween_fire_light():
	fire_light.energy = fire_light_energy
	
	var tween = create_tween()
	tween.tween_property(fire_light, "energy", 0, fire_light_duration) \
	.set_trans(Tween.TRANS_SINE) \
	.set_ease(Tween.EASE_IN).from_current()
	

func tween_fire_gun_body_lamp():
	fire_gun_body_lamp.modulate.a = 1.0
	
	var tween = create_tween()
	tween.tween_property(fire_gun_body_lamp, "modulate:a", 0, fire_light_duration * 1.2) \
	.set_trans(Tween.TRANS_SINE) \
	.set_ease(Tween.EASE_IN).from_current()
	
	
